<?php
/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); ?>
		<div id="primary">
			<div class="container" role="main" id="page-imprensa">
				
				<article>
					<h1 class="entry-title">IMPRENSA</h1>

<?php 
					$args = array(
                        'posts_per_page'  => 5,
                        'offset'          => 0,
                        'category_name'   => 'imprensa',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC',
                        'include'         => '',
                        'exclude'         => '',
                        'meta_key'        => '',
                        'meta_value'      => '',
                        'post_type'       => 'post',
                        'post_mime_type'  => '',
                        'post_parent'     => '',
                        'post_status'     => 'publish',
                        'suppress_filters' => true
                    );
					query_posts($args);
?>
					<div id="box_ano">
						<ul>
							<?php
							global $wpdb;
							$limit = 0;
							$year_prev = null;

							// The Query
							$query = new WP_Query( $args );
							$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month ,	YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_status = 'publish' and post_type = 'post' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 46 GROUP BY month , year ORDER BY post_date DESC");

							//print_r($months);

							foreach($months as $month) :
								$year_current = $month->year;
								if ($year_current != $year_prev){
									if ($year_prev != null){ }
								?>
								<li class="archive-year"><a href="<?php bloginfo('url') ?>/category/imprensa/?ano=<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></li>
								<?php } ?>
								<li><a href="<?php bloginfo('url') ?>/category/imprensa/?ano=<?php echo $month->year; ?>&mes=<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span> (<?php echo $month->post_count; ?>)</a></li>
								<?php
								$year_prev = $year_current;
								if(++$limit >= 18) { break; }
							endforeach;
							?>
						</ul>
					</div>
					<div id="lista-novidades">
						<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

						<div class="entry-content">
							<?php
								if(has_post_thumbnail()){
									the_post_thumbnail( 'thumbnail');
								}
							?>
							<div class="fl">
								<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
								<p>
									<?php echo strip_tags(get_the_excerpt()); ?>
									<a href="<?php the_permalink(); ?>"><span>[Saiba mais]</span></a>
								</p>
							</div>
						</div>

					<?php endwhile;?>
					<?php endif; ?>
					</div>
				</article><!-- #post-<?php the_ID(); ?> -->

				<div class="clear"></div>
			</div><!-- #content -->
		</div><!-- #primary -->
<?php //get_sidebar('page'); ?>
<?php get_footer(); ?>